package com.hh.releasenode.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName Response
 * @Description TODO
 * @Author KunxianLi
 * Date 2020/6/1 17:36
 * Version 1.0
 **/
@Data
public class Response<T> implements Serializable {

    public static final String CODE_SUCCESS = "000000";
    public static final String MSG_SUCCESS = "SUCCESS";

    public static final String CODE_ERROR = "999999";
    public static final String MSG_ERROR = "ERROR";

    private String code;

    private String msg;

    private T data;

    public Response(){

    }

    public static <T> Response<T> result(T data, String code, String msg) {
        Response<T> response = new Response<>();
        response.code = code;
        response.data = (data == null ? (T) new Object() : data);
        response.msg = msg;
        return response;
    }

    public static <T> Response<T> success(T data) {
        return result(data, CODE_SUCCESS, MSG_SUCCESS);
    }

    public static <T> Response<T> success() {
        Response response = new Response<>();
        response.setCode(CODE_SUCCESS);
        response.setMsg(MSG_SUCCESS);
        return response;
    }

    public static Response error(){
        Response response = new Response<>();
        response.setCode(CODE_ERROR);
        response.setMsg(MSG_ERROR);
        return response;
    }

    public static Response error(String mesg){
        Response response = new Response<>();
        response.setCode(CODE_ERROR);
        response.setMsg(mesg);
        return response;
    }
}
